﻿using DevExpress.XtraEditors;
using EkStudio.com.bean.xml.RootInterface;
using EkStudio.com.bean.xml.RootInterface.Field;
using EkStudio.com.bean.xml.Unit;
using EkStudio.com.ui.form;
using EkStudio.com.ui.unitbill.property.Event;
using EkStudio.Com.UI.Interface;
using System.Windows.Forms;

namespace EkStudio.com.ui.unitbill.property
{
    public partial class ModuleUiGroupProperty :XtraUserControl
    {

        private BillFormatModule bean = null;

        log4net.ILog logger = log4net.LogManager.GetLogger(typeof(BillUiGroupToProperty));

        private ProjectRootPanel RootPanel = null;

        private ProjectUnitBillInterface UnitPanel = null;

        private BillUiGroupToProperty group = null;

        private ModuleFieldSelectForm selectForm = null;

        public event KEventHandler CaptionChanged = null;

        public ModuleUiGroupProperty(BillFormatModule _bean,ProjectRootPanel panel, ProjectUnitBillInterface unit)
        {
            InitializeComponent();
            this.bean = _bean;
            this.RootPanel = panel;
            this.UnitPanel = unit;
            this.InitProperty();
        }

        private void InitProperty()
        {
            RepositoryShowModel.Items.AddRange(BeanUtil.TABLEMODEL);
            //初始化编辑器的只读属性
            Field_01_Caption.Properties.ReadOnly = UnitPanel.ReadOnly;
            Field_02_Key.Properties.ReadOnly = UnitPanel.ReadOnly;
            Field_03_Table.Properties.ReadOnly = UnitPanel.ReadOnly;
            Field_04_TableName.Properties.ReadOnly = UnitPanel.ReadOnly;
            Field_05_Enable.Properties.ReadOnly = UnitPanel.ReadOnly;
            Field_06_Visible.Properties.ReadOnly = UnitPanel.ReadOnly;
            Field_07_ReadOnly.Properties.ReadOnly = UnitPanel.ReadOnly;
            Field_08_GroupIndex.Properties.ReadOnly = UnitPanel.ReadOnly;
            Field_09_ShowModel.Properties.ReadOnly = UnitPanel.ReadOnly;
            Field_10_Expanded.Properties.ReadOnly = UnitPanel.ReadOnly;
            Field_11_ExpandField.Properties.ReadOnly = UnitPanel.ReadOnly;
            Field_12_TreeTable.Properties.ReadOnly = UnitPanel.ReadOnly;
            Field_13_TreeField.Properties.ReadOnly = UnitPanel.ReadOnly;
            Field_14_TreeCaption.Properties.ReadOnly = UnitPanel.ReadOnly;
            Field_15_ShowQuery.Properties.ReadOnly = UnitPanel.ReadOnly;
            Field_16_SchemeDefaut.Properties.ReadOnly = UnitPanel.ReadOnly;
            Field_17_Height.Properties.ReadOnly = UnitPanel.ReadOnly;
            Field_18_HasPopMenu.Properties.ReadOnly = UnitPanel.ReadOnly;

        }



        public BillUiGroupToProperty SelectObject
        {
            set {
                group = value;
                group.DataChange += new KEventHandler(PropertyDataChange);
                this.PropertyGrid.SelectedObject = group;
                EvenAction();
            }
        }

        public BillUiGroupToProperty[] SelectObjects
        {
            set {
                group = new BillUiGroupToProperty(value);
                group.DataChange += new KEventHandler(PropertyDataChange);
                this.PropertyGrid.SelectedObject = group;
                EvenAction();
            }

        }


        private void EvenAction()
        {
            if (group.IsMultiple())//当前是多分组
            {
                category_Group_One.Visible = false;
            }
            else
            {
                category_Group_One.Visible = true;
            }
            
            if (group.IsTable)
            {
                //XtraMessageBox.Show("当前是子表");
                Field_03_Table.Expanded = true;
                Field_04_TableName.Visible = true;
                category_Group_Three.Visible = true;
            }
            else
            {
                //XtraMessageBox.Show("当前是主表");
                Field_03_Table.Expanded = false;
                Field_04_TableName.Visible = false;
                category_Group_Three.Visible = false;
            }
        }

        private void RepositoryFieldSelected_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if(selectForm == null) { selectForm = new ModuleFieldSelectForm(); }
            selectForm.SetFormatBill(bean,ModuleFieldSelectForm.ModuleLoadType.LoadTypeMain);
            DialogResult DR =  selectForm.ShowDialog();
            if(DR == DialogResult.OK)
            {
                BillUiFieldsItem item = selectForm.SelectField;
                if (item == null)
                {
                    RootPanel.ShowAlert("错误提示：", "请先选择字段后再确认");
                }
                else
                {
                    group.ExpandField = item.Key;
                    PropertyGrid.Refresh();
                    UnitPanel.SaveStatus = false;
                }
            }
            
        }

        private void PropertyDataChange(object sender, PropertyDataEvent e)
        {
            logger.Info(string.Format("修改字段[{0}]的值为[{1}]",e.Key,e.Value.ToString()));
            if (e.Key == "IsTable")//当前是子表
            {
                
                if (e.Value.Equals(true))
                {
                    Field_03_Table.Expanded = true;
                    Field_04_TableName.Visible = true;
                    category_Group_Three.Visible = true;
                }
                else
                {
                    group.TableName = "";
                    Field_03_Table.Expanded = false;
                    Field_04_TableName.Visible = false;
                    category_Group_Three.Visible = false;
                    PropertyGrid.Refresh();
                }
                
            }
        }

        private void PropertyGrid_CellValueChanged(object sender, DevExpress.XtraVerticalGrid.Events.CellValueChangedEventArgs e)
        {
            UnitPanel.SaveStatus = false;
            if(e.Row.Properties.FieldName == "Caption")
            {
                if (this.CaptionChanged != null)
                {
                    PropertyDataEvent data = new PropertyDataEvent("", e.Value.ToString());
                    this.CaptionChanged(this, data);
                }
            }
        }
        /// <summary>
        /// 设置表格树的Json字段
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void RepositoryTableFieldSelected_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (selectForm == null) { selectForm = new ModuleFieldSelectForm(); }
            selectForm.SetFormatBill(bean, group.Key);
            DialogResult DR = selectForm.ShowDialog();
            if (DR == DialogResult.OK)
            {
                BillUiFieldsItem item = selectForm.SelectField;
                if (item == null)
                {
                    RootPanel.ShowAlert("错误提示：", "请先选择字段后再确认");
                }
                else
                {
                    if(group.TreeField != item.Key)
                    {
                        group.TreeField = item.Key;
                        PropertyGrid.Refresh();
                        UnitPanel.SaveStatus = false;
                    }
                }
            }
        }
        /// <summary>
        /// 设置表格树的节点关系字段
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void RepositoryTreeCaption_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (selectForm == null) { selectForm = new ModuleFieldSelectForm(); }
            selectForm.SetFormatBill(bean, group.Key);
            DialogResult DR = selectForm.ShowDialog();
            if (DR == DialogResult.OK)
            {
                BillUiFieldsItem item = selectForm.SelectField;
                if (item == null)
                {
                    RootPanel.ShowAlert("错误提示：", "请先选择字段后再确认");
                }
                else
                {
                    if(group.TreeCaption != item.Key)
                    {
                        group.TreeCaption = item.Key;
                        PropertyGrid.Refresh();
                        UnitPanel.SaveStatus = false;
                    }
                }
            }
        }
    }
}
